#include "../include/head.h"

void tea_manage()
{
     //创建一个空链表
    Pnode_teacher H=creat_list_tea();
    Pnode_teacher read_H=creat_list_tea();
    read_file_tea(read_H);
    system("clear");
    Pnode_teacher p;
    int ch;
    while (1)
    {
        
        tea_menu();
        scanf("%d",&ch);
        getchar();
        
        switch (ch)
        {
        case 1:
            system("clear");
            inputdata_tea(H);
            break;
        case 2:
            system("clear");
            find_data_file_tea(read_H);
            break;
        case 3:
            system("clear");
            manage_stu();
            system("clear");
            break;
        case 4:
            system("clear");
            printf("输出全部教师信息\n");
            
            p=read_H->next;
            printf("\n%-13s%-13s%-13s%-13s\n", 
           "姓名", "班级", "工号", "科目");
            printf("--------------------------------------------------------------------------------\n");
    
            while (p != read_H) {    
                printf("%-12s%-11s%-11s%-12s\n", 
                p->data.name, p->data.class, p->data.num,
                p->data.sub);
                p=p->next;
                
            }
            break;    
        case 5:
            return ;
            
            break;
        default:
            
            break;
        }
        
    }
}

//教师菜单
void tea_menu(void)
{
    char m[5][100]={"1.录入教师信息","2.查询个人信息","3.管理学生信息","4.查看全部教师信息","5.退出"};
    printf("\n----------------------------\n教师系统\n------------------------\n");
    for (int i=0;i<5;i++)
    printf("%s\n",m[i]);
    printf("----------------------------\n");
}

int curman;
void write_file_tea(Pnode_teacher ph)
{
    printf("写入文件\n");
    FILE *fp=fopen("./student_management/data/tea.txt","w");
    if(fp==NULL){ printf("文件打卡失败\n");return;}

    Pnode_teacher p=ph->next;//头节点不存数据

    while(p!=ph)
    { printf("开始写入\n");
        fwrite(&p->data,sizeof(data_type),1,fp);
        p=p->next;
    }
    fclose(fp);
    printf("文件写入完成\n");

}

void read_file_tea(Pnode_teacher ph)
{
    FILE *fp=fopen("./student_management/data/tea.txt","r");
    if(fp==NULL)
    {
        printf("读取文件打开失败\n");
        curman=0;
        return;
    }
     clear_list_tea(ph);
    curman=0;
    data_type tea;
    while (fread(&tea, sizeof(data_type), 1, fp) == 1) 
    {
        tail_insert_tea(ph, tea);
        curman++;
    }
    printf("curman:%d\n",curman);
    fclose(fp);
}


//输入教师信息
void inputdata_tea(Pnode_teacher ph)
{
    data_type tea;
    //1.输入
    printf("工号 姓名 班级 教学科目");
    scanf("%19s",tea.num);
    getchar();
    scanf("%19s",tea.name);
    getchar();
    scanf("%19s",tea.class);
    getchar();
    scanf("%19s",tea.sub);
    getchar();
        curman++;
    //2.将信息加在链表的表尾
    tail_insert_tea(ph,tea);
  
    //3.写入文件
    write_file_tea(ph);
}

data_type FindputData(Pnode_teacher head,char *findname)
{
    data_type temp={0};
    Pnode_teacher p=head->next;
    for(int i=0;i<curman;i++)
    {
        if(strcmp(p->data.name,findname)==0)
        {
            temp=p->data;
            break;
        }
        p=p->next;
    }
   

    return temp;
}

//从文件查找老师信息
void find_data_file_tea(Pnode_teacher head)
{
    char find_name[20];
    printf("请输入要查询的姓名：");
    scanf("%19s",find_name);
    getchar();
    read_file_tea(head);
    data_type  find_data=FindputData(head,find_name);
    if(strlen(find_data.name)!=0){
        printf("\n%-13s%-13s%-13s%-13s\n", 
           "姓名", "班级", "工号", "科目");
        printf("--------------------------------------------------------------------------------\n");
        printf("%-12s%-11s%-11s%-12s\n",find_data.name,find_data.class,find_data.num,find_data.sub);
    
    }
    else 
    printf("查无此人\n");
}

